/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

//
//bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
//    if (p == NULL && q == NULL)
//        return true;
//
//    if (p == NULL || q == NULL)
//        return false;
//
//    if (p->val != q->val)
//        return false;
//
//    return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
//}


bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
    if (p == NULL && q == NULL)
    {
        return true;
    }
    else if ((p != NULL && q == NULL)
        || (p == NULL && q != NULL))
    {
        return false;
    }

    if ((p->left == NULL && q->left != NULL)
        || (p->left != NULL && q->left == NULL))
    {
        return false;
    }

    if ((p->right == NULL && q->right != NULL)
        || (p->right != NULL && q->right == NULL))
    {
        return false;
    }

    if (p->val == q->val && q != NULL && p != NULL)
    {
        return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
    }
    else if (p->val != q->val && q != NULL && p != NULL)
    {
        return false;
    }

    return true;
}